str2date = function(c_date) {
    if (!c_date)
        return "";
    var tempArray = c_date.split("-");
    if (tempArray.length != 3) {
        alert("你输入的日期格式不正确,正确的格式:2000-05-01 02:54:12");
        return 0;
    }
    var dateArr = c_date.split(" ");
    var date = null;
    if (dateArr.length == 2) {
        var yymmdd = dateArr[0].split("-");
        var hhmmss = dateArr[1].split(":");
        date = new Date(yymmdd[0], yymmdd[1] - 1, yymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]);
    } else {
        date = new Date(tempArray[0], tempArray[1] - 1, tempArray[2], 00, 00, 01);
    }
    return date;
};

getnextdate = function(nowdate){
	var now = new Date(nowdate);
	var yy = now.getFullYear();
	var mm = now.getMonth()+1;
	var dd = now.getDate();
	var hh = now.getHours();
	var min = now.getMinutes();
	return str2date(yy+"-"+mm+"-"+dd+" "+hh+":"+min+":00");
};

var nowDate =document.getElementById("nowDate").value;
var myDate = str2date(nowDate);
var begin_time =document.getElementById("begin_time").value;
var the_begin_time = str2date(begin_time);

var nextbegin_time =document.getElementById("nextbegin_time").value;
var nextbegin_time = str2date(nextbegin_time);

var begin_time2 =document.getElementById("begin_time2").value;
var the_begin_time2 = str2date(begin_time2);

var nextbegin_time2 =document.getElementById("nextbegin_time2").value;
var nextbegin_time2 = str2date(nextbegin_time2);

var end_time =document.getElementById("end_time").value;
var the_end_time = str2date(end_time);

function change() {
	myDate = Number(myDate) + 1000; //当前时间
	if(myDate<the_end_time){
		//100M流量
		var surplus101 = Number(document.getElementById("surplus101").value);
		if(myDate>the_begin_time&&surplus101>0){
			document.getElementById("miaoshabutton").innerHTML = "<a href=\"javascript:;\" class=\"cbtn2\" onclick=\"flowSeckill(1)\"/>0元秒杀</a>";
			document.getElementById("cjcontent1").innerHTML = "<p class=\"prizeCount\"><span class=\"redFont\">当前时段奖品剩余："+surplus101+"个</span></p>";
		}else{
			document.getElementById("miaoshabutton").innerHTML = "秒杀未开始";
			document.getElementById("cjcontent1").innerHTML = "<p class=\"prizeNext\">距下次秒杀还剩：<br/><span class=\"redFont\"><span id=\"showxmtime\"></span></span></p>";
			var seconds = (the_begin_time - myDate)/1000;
		    var minutes = Math.floor(seconds/60);
		    var hours = Math.floor(minutes/60);
		    var days = Math.floor(hours/24);
		    var day= days;
		    var hour= hours % 24;
		    var min= minutes % 60;
		    var sec= seconds % 60;

			if (day < 0 || hour < 0 || min < 0 || sec < 0) {
				day = 0;
				hour = 0;
				min = 0;
				sec = 0;
				document.getElementById("showxmtime").innerHTML = day + "天" + hour
						+ "时" + min + "分" + sec + "秒";
				if(surplus101>0){
					document.getElementById("miaoshabutton").innerHTML = "<a href=\"javascript:;\" class=\"cbtn2\" onclick=\"flowSeckill(1)\"/>0元秒杀</a>";
					document.getElementById("cjcontent1").innerHTML = "<p class=\"prizeCount\"><span class=\"redFont\">当前时段奖品剩余："+surplus101+"个</span></p>";
					clearInterval(djs);
				}else{
					var now = new Date(myDate);
					var end = new Date(the_end_time);
					var dd = now.getDate();
					var hh = now.getHours();
					var end_dd = end.getDate();
					alert("now="+now+"  end="+end+"  dd="+dd+"   hh="+hh+"  end_dd="+end_dd);
					if(hh==23&&end_dd==dd+1){
						document.getElementById("miaoshabutton").innerHTML = "秒杀已结束";
						document.getElementById("miaoshabutton2").innerHTML = "秒杀已结束";
						$("#prizeInfo1").hide();
						document.getElementById("cjcontent1").innerHTML ="本次秒杀活动已结束，下次杀活动，敬请关注。";
					}else{
						document.getElementById("surplus101").value="100";
						if(the_begin_time>=nextbegin_time){
							the_begin_time = new Date(nextbegin_time.getTime()+30*60*1000);
						}else{
							the_begin_time = nextbegin_time;
						}
					}
				}
			} else {
				document.getElementById("showxmtime").innerHTML = day + "天" + hour
						+ "时" + min + "分" + sec + "秒";
			}
		}
		
		 
		
	}else{
		document.getElementById("miaoshabutton").innerHTML = "秒杀已结束";
		document.getElementById("miaoshabutton2").innerHTML = "秒杀已结束";
		$(".prizeInfo").hide();
		document.getElementById("cjcontent1").innerHTML ="本次秒杀活动已结束，下次杀活动，敬请关注。";
		document.getElementById("cjcontent2").innerHTML ="本次秒杀活动已结束，下次杀活动，敬请关注。";
	}
}

var djs = setInterval("change()", 1000);

function flowSeckill(type){
	if(type=="1"){
		document.getElementById("miaoshabutton").innerHTML = "0元秒杀";
	}else{
		document.getElementById("miaoshabutton2").innerHTML = "0元秒杀";
	}
	var contentPath="";
	$.ajax({url:contentPath+'http://localhost/jeesite/f/wechat/product/doseckill',data:{"flowseckilltype":type},
		success:function(result){
			alert('Success...');
			var obj = JSON.parse(result); 
			alert(obj.message);
			if(type=="1"){
				document.getElementById('surplus101').value=obj.num;
				document.getElementById("miaoshabutton").innerHTML = "<a href=\"javascript:;\" class=\"cbtn2\" onclick=\"flowSeckill(1)\"/>0元秒杀</a>";
			}else{
				document.getElementById('surplus102').value=obj.num;
				document.getElementById("miaoshabutton2").innerHTML = "<a href=\"javascript:;\" class=\"cbtn2\" onclick=\"flowSeckill(2)\"/>0元秒杀</a>";
			}
		}
	});
};
